
package com.amado.sprintboot.redscache.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.amado.sprintboot.redscache.entity.User;
import com.amado.sprintboot.redscache.service.UserService;

/**
 * @author wangyl-910
 * @description
 * @date 2018/2/26
 * @see
 */
@Service
public class UserServiceOriginalRedisImpl implements UserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceOriginalRedisImpl.class);
	@Autowired
	private RedisTemplate<Object, Object> redisTemplate;

	@Override
	@Cacheable(cacheNames = "user", key = "#userId")
	public User getUser(String userId) {
		Object obj = redisTemplate.opsForValue().get(userId);
		User user = null;
		if (obj == null) {
			LOGGER.info("getUser USER:{}", user);
			user = new User();
			user.setUserId(userId);
			user.setName("name");
		}
		return user;
	}

	@Override
	@CacheEvict(cacheNames = "user", key = "#userId")
	public void deleteUser(String userId) {
		LOGGER.info("deleteUser USER:{}", userId);
	}

	@Override
	@CacheEvict(cacheNames = "user", key = "#record.userId")
	public void upateUser(User record) {
		LOGGER.info("deleteUser USER:{}", record);
	}
}
